using System.Drawing;

namespace GDI
{
    public class PrintRegion : PrintableItem
    {
        /* -------------------------------------------------------------------------------------------- */

        private Pen f_LinePen;

        private readonly SolidBrush f_FillBrush = new SolidBrush(Color.Transparent);

        private PointF[] f_Points;

        /* -------------------------------------------------------------------------------------------- */

        public Pen LinePen
        {
            get { return f_LinePen; }
            set { f_LinePen = value; }
        }

        public Color FillColor
        {
            get { return f_FillBrush.Color; }
            set { f_FillBrush.Color = value; OnPropertyChanged(); }
        }

        public PointF[] Points { get { return f_Points; } set { f_Points = value; OnPropertyChanged(); } }

        public PointF Center
        {
            get
            {
                var lv_Result = new PointF();
                if(f_Points == null || f_Points.Length == 0) return lv_Result;
                var i_Length = f_Points.Length;
                for(var i = 0; i < i_Length; i++)
                {
                    var p = f_Points[i];
                    lv_Result.X += p.X;
                    lv_Result.Y += p.Y;
                }
                lv_Result.X /= f_Points.Length;
                lv_Result.Y /= f_Points.Length;
                return lv_Result;
            }
            set
            {
                if(f_Points == null || f_Points.Length == 0) return;
                var lv_OldCenter = Center;
                var lv_NewCenter = value;
                var lv_Delta = lv_NewCenter.Substract(lv_OldCenter);
                var i_Length = f_Points.Length;
                for(var i = 0; i < i_Length; i++)
                    f_Points[i] = f_Points[i].Add(lv_Delta);
                OnPropertyChanged();
            }
        }

        public SizeF Size
        {
            get
            {
                var lv_Result = new SizeF();
                if(f_Points == null || f_Points.Length == 0) return lv_Result;
                var lv_MinX = float.PositiveInfinity;
                var lv_MinY = lv_MinX;
                var lv_MaxX = float.NegativeInfinity;
                var lv_MaxY = lv_MaxX;
                var i_Length = f_Points.Length;
                PointF p;
                for(var i = 0; i < i_Length; i++)
                {
                    p = f_Points[i];
                    if(p.X < lv_MinX) lv_MinX = p.X;
                    if(p.X > lv_MaxX) lv_MaxX = p.X;
                    if(p.Y < lv_MinY) lv_MinY = p.Y;
                    if(p.Y > lv_MaxY) lv_MaxY = p.Y;
                }
                lv_Result = new SizeF(lv_MaxX - lv_MinX, lv_MaxY - lv_MinY);
                return lv_Result;
            }
            //set
            //{
            //    if(f_Points == null || f_Points.Length == 0) return;
            //    SizeF OldSize = Size;
            //    SizeF NewSize = value;
            //    SizeF Delta = NewSize.Substract(OldSize);
            //    Delta = Delta.Devide(2);
            //    for(int i = 0, i_Length = f_Points.Length; i < i_Length; i++)
            //    {
            //        f_Points[i] = f_Points[i].s
            //    }
            //}
        }


        /* -------------------------------------------------------------------------------------------- */


        /* -------------------------------------------------------------------------------------------- */

        protected override void Draw(Graphics g)
        {
            if(Points == null) return;
            g.DrawPolygon(f_LinePen, f_Points);
            //g.DrawLines(f_LinePen, f_Points);
            if(f_FillBrush.Color != Color.Transparent)
                g.FillPolygon(f_FillBrush, f_Points);
        }

        /* -------------------------------------------------------------------------------------------- */


        /* -------------------------------------------------------------------------------------------- */
    }
}